<?php
include_once("model/paramtersBean.class.php");
include_once("model/orderBean.class.php");
include_once("controller/Paging.class.php");
require_once("includes/util.php");

/**
 * dashboard class. This is a class used to process all
 * logic of dashboard tab in admin page.
 */
class paramters 
{
	var $db;
 	var $smarty;
 	
    function paramters($db, $smarty)
	{
		//store pointer to db onject
		$this->db = $db;
		$this->smarty = $smarty;
	}
	
	/**
	 * This function will be call be index.php base on mod specified by GET param
	 */
	function process(){
		$paramtersBean = new paramtersBean($db, $smarty);
		$orderBean = new orderBean($db, $smarty);
		if(isset($_POST['act']) && $_POST['act'] == 'newfee'){
				$this->newfee();	
		}else{
			if(isset($_GET['sub1']))
				$sub1 = $_GET['sub1'];
			if(isset($_GET['act']))
				$getact = $_GET['act'];	
			if($sub1 == 'order-buy'){
				
			}else{
				if($getact == 'new' || $getact == 'edit' ){
					$allSrcEcurrency=$orderBean->getSrcECurrency($this->db);
					$allEcurrency=$orderBean->getECurrency($this->db);
					$this->smarty->assign("ALLSRCECURRENCY",$allSrcEcurrency);
					$this->smarty->assign("ALLECURRENCY",$allEcurrency);	
				}else{
					$countsellfee=$paramtersBean->countFee($this->db,'1');
					$sellfee['data']=$paramtersBean->getFeeShare($this->db,MAX_DEMO_PRODUCT_ADMIN,"list",'1');
					$link = "?mod=".$_GET['mod']."&sub1=".$_GET['sub1'];
					$resultsellfee = Paging::do_paging($countsellfee,$sellfee, MAX_DEMO_PRODUCT_ADMIN, $link,"list");
					$resultsellfee['PATH'] = "../".UPLOAD_DIR_LOGO;
					$this->smarty->assign("ALLSELLFEE",$resultsellfee);	
					$countbuyfee=$paramtersBean->countFee($this->db,'2');
					$buyfee['data']=$paramtersBean->getFeeShare($this->db,MAX_DEMO_PRODUCT_ADMIN,"list",'2');
					
					$link = "?mod=".$_GET['mod']."&sub1=".$_GET['sub1'];
					$resultbuyfee = Paging::do_paging($countbuyfee,$buyfee, MAX_DEMO_PRODUCT_ADMIN, $link,"list");
					$resultbuyfee['PATH'] = "../".UPLOAD_DIR_LOGO;
					$this->smarty->assign("ALLBUYFEE",$resultbuyfee);	
				}
			}
			$this->smarty->display('paramters.tpl');
		}
	}
	
	public function newFee() {
			$type = array();
			$arr = array( 'ecurrency_id' => $_POST['ecurrency_id'],
					  'bank_id' => $_POST['bank_id'],								   
					  'fee_percent' => $_POST['sell_fee'],
					  'bank_fee' => $_POST['bank_fee'],
					  'amount_litmit' => $_POST['current_limit'],
					  'currency_type' => $_POST['currency_type']);
			$ret = "";		
			$ret = $this->validateFee($arr);
			if($ret['dem'] == 0) {					
				$id = paramtersBean::saveFee($this->db, $arr);
				if($id > 0) {
					$type = array('ID' => 0);
					
					$type = array('ID' => 0, 'message' => 'Succecfull','dem'=>$ret['dem']);
					echo array2json($type);	
				}else{	
					$type = array('ID' => -1, 'message' => $ret);
					echo array2json($type);		
				}			
			} else {
				$type = array('ID' => -1, 'message' => 'Please enter (*)','detail'=>$ret,'dem'=>$ret['dem']);
				echo array2json($type);	
			}
	}
	
	public function validateFee($arr)
    {
    	$msg = array();
		$msg["ecurrency_name"] = '';
		$msg["sell_fee"] = '';
		$msg["bank_fee"] = '';
		$msg["current_limit"] = '';
		
		$dem =0;
		
		if(isset($arr['ecurrency_name']) && $arr['ecurrency_name'] == ""){
			$msg["ecurrency_name"]= "Your buy src ecurrency could not be empty.";
			$dem++;
		}
		
		if(isset($arr['sell_fee']) && $arr['sell_fee'] == ""){
			$msg["sell_fee"]= "Your buy dst amount could not be empty.";
			$dem++;
		}
		
		if(isset($arr['bank_fee']) && $arr['bank_fee'] == ""){
			$msg["bank_fee"]= "Your buy dst ecurrency could not be empty.";
			$dem++;
		}
		
		if(isset($arr['current_limit']) && $arr['current_limit'] == ""){
			$msg["current_limit"]= "Your buy dst ecurrency could not be empty.";
			$dem++;
		}
		
		$msg["dem"] = $dem;
		return $msg;
    }
	
}
?>